/**
 * 自定义jQuery插件
// if(!/^[\u4E00-\u9FFF_a-zA-Z]{2,15}$/.test(name)){alert("用户名由2-15位汉字或英文组成!")}
// if(!/^1[3|4|5|7|8]\d{9}$/.test(mobile)){alert("手机号码输入有误!")}
// if(!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(id_email)){alert("邮箱格式不正确!")}
// if(!/^[a-z][a-z0-9_-]{7,14}$/.test(id_password)){alert("密码格式不正确!")}
 */

(function($){
	/**
	 * 类插件  $.lixing.checkEmail(email)
	 */
	$.lixing = {
		getWebRootName: function(){
			var pathName = window.location.pathname.substring(1);
			var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
			//return window.location.protocol + '//' + window.location.host + '/'+ webName + '/';
			return '/'+webName;
		},
		getUrl: function(url){
			var pathName = window.location.pathname.substring(1);
			var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
			return '/'+webName+url;
		},
		countStrLength: function(str){
			var length = 0;
			var arr = str.split("");
			for(var i = 0 ; i < arr.length ; i++){
				if(arr[i].charCodeAt(0) < 299){
					length ++;
				}else{
					length += 2;
				}
			}
			return length;
		},
		checkEmail: function(email){
			return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email)
		},
		checkMobilePhone: function(mobilePhone){
			return /^1[3|4|5|7|8]\d{9}$/.test(mobilePhone)
		},
		checkPassword: function(password,regular){
			if(regular == null){
				return /^[a-z][a-z0-9_-]{7,14}$/.test(password)
			}else{
				return regular.test(password)
			}
		},
		checkAccount: function(account,regular){
			if(regular == null){
				return /^[\u4E00-\u9FFF_a-zA-Z]{2,15}$/.test(account)
			}else{
				return regular.test(account)
			}
		},
		// checkDateStr: function(dateStr){
		// 	if(regular == null){
		// 		return /^([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))$/.test(dateStr)
		// 	}else{
		// 		return regular.test(dateStr)
		// 	}
		// },
		checkCode: function(code,regular){
			if(regular == null){
				return /^[a-zA-Z]{4}$/.test(code)
			}else{
				return regular.test(code)
			}
		},
	};
	/**
	 * 实例插件 $("#id").serializeFromToObject()
	 */
	$.fn.extend({
		serializeFromToObject: function(){ 
			var o = {};
			$.each(this.serializeArray(),function(index){
				if (o[this['name']]) {
					o[this['name']] = o[this['name']] + "," + this['value'];
				}else{
					o[this['name']] = this['value'];
				}
			});
			return o;
		},
		serializeObject: function(){ 
		    var obj = {};
		    var count = 0;
		    $.each( this.serializeArray(), function(i,o){
		        var n = o.name, v = o.value;
		        count++;
		        obj[n] = obj[n] === undefined ? v
		        : $.isArray( obj[n] ) ? obj[n].concat( v )
		        : [ obj[n], v ];
		    });
		    //obj.nameCounts = count + "";//表单字段的个数
		    //return JSON.stringify(obj);//返回JSON字符串
		    return obj;
		},
		serializeJson: function(){ 
		    var serializeObj = {};  
		    var array = this.serializeArray();  
		    var str = this.serialize();  
		    $(array).each(function(){  
		        if(serializeObj[this.name]){  
		            if($.isArray(serializeObj[this.name])){  
		                serializeObj[this.name].push(this.value);  
		            }else{  
		                serializeObj[this.name]=[serializeObj[this.name],this.value];  
		            }  
		        }else{  
		            serializeObj[this.name]=this.value;   
		        }  
		    });  
		    //return JSON.stringify(serializeObj);//返回JSON字符串
		    return serializeObj; 
		}
	});
})(jQuery);


